package com.ezops.poc.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Class representing the Dynamic model.
 * 
 * @author vgaurav
 * 
 */
public class DefaultBaseDynamicModel implements DynamicModel {

	private Map<String, String> attributes;

	protected List<Field> fields;

	@Override
	public Iterator<Field> iterator() {
		if (this.fields == null || fields.isEmpty()) {
			throw new IllegalStateException(
					"There is no attributes present in the client model.");
		}
		return fields.iterator();
	}

	@Override
	public String[] getHeaders() {

		if (this.attributes == null || attributes.isEmpty()) {
			throw new IllegalStateException(
					"There is no attributes present in the response.");
		}

		return attributes.keySet().toArray(new String[attributes.size()]);
	}

	@Override
	public void addField(String fieldName, String fieldValue) {

		if (fieldName == null || fieldName.isEmpty()) {
			throw new IllegalArgumentException(
					"The field name can not be blank.");
		}
		if (attributes == null) {
			attributes = new HashMap<String, String>();
		}
		attributes.put(fieldName, fieldValue);
		if (this.fields == null)
			this.fields = new ArrayList<>();
		fields.add(Field.getInstance(fieldName, fieldValue));
	}

	@Override
	public String getField(String fieldName) {
		if (this.attributes == null)
			return null;
		return this.attributes.get(fieldName);
	}

}
